from django.db import models
import pytz
# Create your models here.
from dj31.utils.model import BaseModel
'''
1.分类导航
2.轮播图 图片 优先级
3.热门文章 
4.图片  标题 文章 链接 时间 作者 点赞量


'''

class Tag(BaseModel):
    name = models.CharField(max_length=60,verbose_name='分类名字')

    class Meta:
        ordering = ['-update_time','-id']
        db_table = 'news_tag'
        verbose_name = '分类'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class Banner(BaseModel):
    image_url = models.URLField(verbose_name='图片地址')
    priority = models.IntegerField(verbose_name='优先级')
    news = models.OneToOneField('News',on_delete=models.CASCADE) #人在剑在


    class Meta:
        ordering = ['-update_time','-id']  #排序
        db_table = 'tb_banner'
        verbose_name = '轮播图'
        verbose_name_plural = verbose_name

    def __str__(self):
        return '<轮播图{}>'.format(self.id)



class News(BaseModel):
    title = models.CharField(max_length=64,verbose_name='标题')
    digest = models.CharField(max_length=200,verbose_name='摘要')
    content = models.TextField(verbose_name='内容')
    clicks = models.IntegerField(verbose_name='点击量',default=0)
    image_url = models.URLField(verbose_name='图片地址',default='')
    tag = models.ForeignKey('Tag',verbose_name='分类',on_delete=models.SET_NULL,null=True)#可以为空
    author = models.ForeignKey('users.Users',verbose_name='作者',on_delete=models.SET_NULL,null=True)

    class Meta:
        ordering = ['-update_time','-id']  #排序，先看时间再看id
        db_table = 'tb_news'
        verbose_name = '新闻'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.title

    def in_clicks(self):
        self.clicks += 1
        self.save(update_fields=['clicks']) #指定更改



class HotNew(BaseModel):
    news = models.OneToOneField('News',on_delete=models.CASCADE)
    priority = models.IntegerField(verbose_name='优先级')

    class Meta:
        ordering = ['-update_time','-id']  #排序
        db_table = 'tb_hotnews'
        verbose_name = '热门新闻'
        verbose_name_plural = verbose_name

    def __str__(self):
        return '<热门新闻{}>'.format(self.id)


class Coment(BaseModel):
    content = models.TextField(verbose_name='评论内容')
    author = models.ForeignKey('users.Users',on_delete=models.SET_NULL,null=True)
    news = models.ForeignKey('News',on_delete=models.CASCADE)
    parent = models.ForeignKey('self',on_delete=models.CASCADE,blank=True,null=True)#blank表单可以为空


    class Meta:
        db_table='tb_comment'
        verbose_name='评论'
        verbose_name_plural=verbose_name

    def __str__(self):
        return '评论{}'.format(self.id)


    def to_dict(self):
        ss = pytz.country_timezones('cn')
        cn = pytz.timezone(ss[0]) #上海时间
        news_time = cn.normalize(self.update_time)  #转亚洲时间

        comm_dict = {
            'news_id':self.news.id,
            'content_id':self.id,
            'content':self.content,
            'author':self.author.username,
            'update_time':self.update_time,
            # 'update_time':self.update_time.strftime('%Y年%m月%d日 %H:%M'),
            # 'update_time':news_time.strftime('%Y年%m月%d日 %H:%M'),
            'parent':self.parent.to_dict() if self.parent_id else None,
        }
        return comm_dict





